﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FirstController : MonoBehaviour, ISceneController, IUserAction
{
    public ActionManager actionManager;
    public DiskFactory diskFactory;                      
    RoundController roundController;
    UserGUI userGUI;

    void Start()
    {
        LoadResources();
    }

    // 加载各组件
    public void LoadResources()
    {
        SSDirector.GetInstance().CurrentScenceController = this;
        gameObject.AddComponent<DiskFactory>();
        gameObject.AddComponent<ActionManager>();
        gameObject.AddComponent<PhysisActionManager>();
        gameObject.AddComponent<RoundController>();
        gameObject.AddComponent<UserGUI>();

        diskFactory = Singleton<DiskFactory>.Instance;
        roundController = Singleton<RoundController>.Instance;
        userGUI = Singleton<UserGUI>.Instance;
    }
    
    // 处理用户的点击动作
    public void Hit(Vector3 position)
    {
        Camera ca = Camera.main;
        Ray ray = ca.ScreenPointToRay(position);

        RaycastHit[] hits;
        hits = Physics.RaycastAll(ray);

        for (int i = 0; i < hits.Length; i++)
        {
            RaycastHit hit = hits[i];
            // 如果用户点击到飞碟
            if (hit.collider.gameObject.GetComponent<DiskData>() != null)
            {
                // 将飞碟移至底部，触发飞行动作的回调
                hit.collider.gameObject.transform.position = new Vector3(0, -7, 0);
                // 分数增加
                roundController.Record(hit.collider.gameObject.GetComponent<DiskData>());
                // 更新GUI
                userGUI.SetPoints(roundController.GetPoints());
            }
        }
    }
    
    // 重置游戏
    public void Restart()
    {
        userGUI.SetResult("");
        userGUI.SetPoints(0);
        userGUI.SetRound(1);
        userGUI.SetMode(1);
        userGUI.SetFlyMode(0);
        roundController.Reset();
    }
    
    // 设置难度模式
    public void SetMode(int mode)
    {
        roundController.SetMode(mode);
    }

    // 设置飞碟的飞行模式
    public void SetFlyMode(int flyMode){
        roundController.SetFlyMode(flyMode);
    }

    // 释放飞碟对象
    public void FreeDisk(GameObject disk){
        diskFactory.FreeDisk(disk);
    }

    void Update()
    {
       
    }
}
